<?php
/*
  * @author 小白 <chuanwei1234@163.com>  
  * @QQ 86844077  
  * @date 2013-09-12
*/

@session_start();
@define("DEBUG_MODE",0);
@date_default_timezone_set('PRC'); //东八时区
error_reporting(7);
set_magic_quotes_runtime(0);

$speed_headtime=explode(' ',microtime());
$speed_headtime=$speed_headtime[0]+$speed_headtime[1];

if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

if(!get_magic_quotes_gpc()){
	$_POST=Add_S($_POST);
	$_GET=Add_S($_GET);
	$_COOKIE=Add_S($_COOKIE);
}
function Add_S($array){
	foreach($array as $key=>$value){
		if(!is_array($value)){
			$array[$key]=addslashes($value);
		}else{
			$array[$key]=Add_S($array[$key]);
		}
	}
	return $array;
}

if(!ini_get('register_globals')){
	@extract($_COOKIE,EXTR_SKIP);
	@extract($_FILES,EXTR_SKIP);
}

foreach($_POST as $_key=>$_value){
	!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
}
foreach($_GET as $_key=>$_value){
	!ereg("^\_[A-Z]+",$_key) && $$_key=$_GET[$_key];
}
define('MX_PATH', substr(dirname(__FILE__), 0, -4).'/');

require_once(MX_PATH."inc/config.php");
require_once(MX_PATH."inc/mysql.php");
global $db;
$db=new mysql();
$db->link(DB_SEREVER,DB_DBNAME,DB_USER,DB_PASSWORD,DB_PORT);

global $webdb;
$webrs=$db->excu("select * from mx_config");
if ($db->num_rows($webrs)>0){
	while ($webrsdb=$db->fetch_array($webrs)){
		$webdb[$webrsdb["c_key"]]=addslashes($webrsdb["c_value"]);
	}
}


require_once(MX_PATH."inc/function.php");
require(MX_PATH."inc/class.inc.php");
require_once(MX_PATH."inc/jpush.php");


global $FROMURL;
$PHP_SELF_TEMP=$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$PHP_SELF=$_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:$PHP_SELF_TEMP;
$HTTP_HOST=$_SERVER['HTTP_HOST']?$_SERVER['HTTP_HOST']:$HTTP_SERVER_VARS['HTTP_HOST'];
$WEBURL='http://'.$HTTP_HOST.$PHP_SELF;
$FROMURL=$_SERVER["HTTP_REFERER"]?$_SERVER["HTTP_REFERER"]:$HTTP_SERVER_VARS["HTTP_REFERER"];
if($webdb['web_open']==0 and strpos("$PHP_SELF","mxsoft")<=0){
    @header('Content-Type:text/html;charset= utf-8');
    echo $webdb['close_why'];
	exit();
}

$truepath=preg_replace("/(.+)\/([^\/]+)\/([^\/]+)/is","\\1",'http://'.$HTTP_HOST);

$webrs=$db->excu("select * from mx_vpp_config");
if ($db->num_rows($webrs)>0){
	while ($webrsdb=$db->fetch_array($webrs)){
		$webdb_vpp[$webrsdb["c_key"]]=$webrsdb["c_value"];
	}
}
//判断机构
if($webdb["vpp_code"]!=md5( $webdb["vpp_jigoucode"]."MX".date("Y",strtotime($VPPCREATDATE))."WEI".date("m",strtotime($VPPCREATDATE))."MX".date("d",strtotime($VPPCREATDATE)))){
	header('Content-Type:text/html;charset= utf-8');
	echo '<span style="color:#FF0000">机构验证失败，请联系微信营销平台工作人员</span>';
	exit();
}


$FROMURL=filtrate($FROMURL);
$WEBURL=filtrate($WEBURL);
global $fun;
$fun=new myfunction;
global $Guidedb;
$Guidedb=new Guide_DB;

//获取浏览器
$browser=$_SERVER['HTTP_USER_AGENT'];
$browserie=0;
$mxsofturl=$webdb[weburl]."/mxsoft/";

if (((strpos(strtolower($browser),'window') and !strpos(strtolower($browser),'windows phone') and !strpos(strtolower($browser),'iemobile')) or strpos(strtolower($browser),'macintosh')) and substr($WEBURL,0,strlen($mxsofturl))!=$mxsofturl){
	$browserie=1;
	echo "<script>location.href='http://www.mxsoft.cn';</script>";
	exit();
}

//更新用户登录状态
if (trim($wxmxopenid)=="" or trim($_COOKIE["mxadminwx"]["wxmxopenid"])!=""){
	$wxmxopenid=trim($_COOKIE["mxadminwx"]["wxmxopenid"]);
}
$vpp_user_info="";
if ($wxmxopenid!=""){
	$vpp_user_info=$db->getinfo("select * from mx_user where weixinid='$wxmxopenid' limit 1");
	if (is_array($vpp_user_info)){
		//更新用户信息
	   	setcookie("mxadminwx[wxmxopenid]",$vpp_user_info["weixinid"],time()+3600*24*7,"/");
	   	setcookie("mxadminwx[userid]",$vpp_user_info["id"],time()+3600*24*7,"/");
		if (time()-strtotime($vpp_user_info["lastlogintime"])>1800){
			wx_update_userinfo($vpp_user_info["id"]);
		}
		else{
			$vpp_user_info="";	
		}
	}
	else{
		$wxmxopenid="";
	}
}


//更新统计功能
$get_tj_lastdate=$db->getinfo("select tjdate from mx_tj_user order by tjdate desc,id desc");
if (is_array($get_tj_lastdate)){
	$tj_lastdate=date("Y-m-d",strtotime($get_tj_lastdate["tjdate"])+86400);
}else{
	//$tj_lastdate=date("Y-m-d");
	$tj_lastdate="2013-09-07";
}
if(isdate($tj_lastdate)){
	$tj_datearr=get_dayarr($tj_lastdate,date("Y-m-d",time()));
	if (is_array($tj_datearr)){
		foreach ($tj_datearr as $key =>$value){
			$vpp_add=$db->listcount("mx_user"," and guandate>='$value 00:00:00' and guandate<='$value 23:59:59' ");
			$vpp_cancel=$db->listcount("mx_user"," and cguandate>='$value 00:00:00' and cguandate<='$value 23:59:59' ");
			$vpp_jingadd=$vpp_add-$vpp_cancel;
			$vpp_zong1=$db->listcount("mx_user"," and guandate<='$value 23:59:59'");
			$vpp_zong2=$db->listcount("mx_user"," and cguandate<='$value 23:59:59'");
			$vpp_zong=$vpp_zong1-$vpp_zong2;
			$app_add=$db->listcount("mx_app_user"," and regdate>='$value 00:00:00' and regdate<='$value 23:59:59' ");
			$app_cancel=0;
			$app_jingadd=$app_add;
			$app_zong=$db->listcount("mx_app_user"," and regdate<='$value 23:59:59' ");
			$tjzong=$vpp_zong+$app_zong;
			$db->excu("insert into mx_tj_user(tjdate,vpp_add,vpp_cancel,vpp_jingadd,vpp_zong,app_add,app_cancel,app_jingadd,app_zong,tjzong)values('$value','$vpp_add','$vpp_cancel','$vpp_jingadd','$vpp_zong','$app_add','$app_cancel','$app_jingadd','$app_zong','$tjzong')");
		}
	}
}

//更新消息操作
$get_tj_lastdate=$db->getinfo("select tjdate from mx_tj_msg order by tjdate desc,id desc");
if (is_array($get_tj_lastdate)){
	$tj_lastdate=date("Y-m-d",strtotime($get_tj_lastdate["tjdate"])+86400);
}else{
	//$tj_lastdate=date("Y-m-d");
	$tj_lastdate="2013-09-07";
}
if(isdate($tj_lastdate)){
	$tj_datearr=get_dayarr($tj_lastdate,date("Y-m-d",time()));
	if (is_array($tj_datearr)){
		foreach ($tj_datearr as $key =>$value){
			$vpp=$db->listcount("mx_tuisong_vpp_log"," and addtime>='".strtotime($value." 00:00:00")."' and addtime<='".strtotime($value." 23:59:59")."' ");
			$app=$db->listcount("mx_tuisong_app_log"," and addtime>='".strtotime($value." 00:00:00")."' and addtime<='".strtotime($value." 23:59:59")."' ");
			$reply=$db->listcount("mx_tuisong_reply_log"," and addtime>='".strtotime($value." 00:00:00")."' and addtime<='".strtotime($value." 23:59:59")."' ");
			$tjzong=$vpp+$app+$reply;
			$db->excu("insert into mx_tj_msg(tjdate,vpp,app,reply,tjzong)values('$value','$vpp','$app','$reply','$tjzong')");
		}
	}
}

?>